﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


//魔域游戏封包-[msgserver.exe 加密方式]
//2015.7.23
//2015.7.29 
//更新key,变量与函数也不应该是静态..
//2015.8.4 修正更新密码本函数-
namespace GameBase.Network
{
    public class GamePacketKeyEx
    {
        public GamePacketKeyEx()
        {
            m_EncodeIndex = 0;
            m_DecodeIndex = 0;
            mKey1 = mKey2 = 0;
        }

        ~GamePacketKeyEx()
        {

        }
        private int mKey1;
        private int mKey2;
        private ushort m_EncodeIndex;
        private ushort m_DecodeIndex;
        private static byte[] m_Key = {154, 113, 158, 101, 178, 233, 150, 189, 10, 161, 206, 85, 162, 153, 70, 45, 122, 
                                       209, 254, 69, 146, 73, 246, 157, 234, 1, 46, 53, 130, 249, 166, 13, 90, 49, 94, 
                                       37, 114, 169, 86, 125, 202, 97, 142, 21, 98, 89, 6, 237, 58, 145, 190, 5, 82, 9, 
                                       182, 93, 170, 193, 238, 245, 66, 185, 102, 205, 26, 241, 30, 229, 50, 105, 22, 61, 
                                       138, 33, 78, 213, 34, 25, 198, 173, 250, 81, 126, 197, 18, 201, 118, 29, 106, 129, 
                                       174, 181, 2, 121, 38, 141, 218, 177, 222, 165, 242, 41, 214, 253, 74, 225, 14, 149, 
                                       226, 217, 134, 109, 186, 17, 62, 133, 210, 137, 54, 221, 42, 65, 110, 117, 194, 57, 
                                       230, 77, 154, 113, 158, 101, 178, 233, 150, 189, 10, 161, 206, 85, 162, 153, 70, 45, 
                                       122, 209, 254, 69, 146, 73, 246, 157, 234, 1, 46, 53, 130, 249, 166, 13, 90, 49, 94, 
                                       37, 114, 169, 86, 125, 202, 97, 142, 21, 98, 89, 6, 237, 58, 145, 190, 5, 82, 9, 182,
                                       93, 170, 193, 238, 245, 66, 185, 102, 205, 26, 241, 30, 229, 50, 105, 22, 61, 138, 33,
                                       78, 213, 34, 25, 198, 173, 250, 81, 126, 197, 18, 201, 118, 29, 106, 129, 174, 181, 2, 
                                       121, 38, 141, 218, 177, 222, 165, 242, 41, 214, 253, 74, 225, 14, 149, 226, 217, 134, 
                                       109, 186, 17, 62, 133, 210, 137, 54, 221, 42, 65, 110, 117, 194, 57, 230, 77, 126, 222,
                                       254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 
                                       222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 
                                       126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254,
                                       94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 
                                       254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 
                                       222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 
                                       126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 
                                       94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 
                                       254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 
                                       222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 
                                       126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 
                                       94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 
                                       254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 222, 254, 94, 126, 
                                       222, 254, 94, 126, 222, 254, 94, 228, 175, 96, 59, 204, 55, 104, 227, 116, 127, 48, 11, 
                                       220, 71, 184, 115, 4, 15, 0, 27, 236, 151, 8, 195, 148, 223, 208, 107, 252, 39, 88, 83,
                                       36, 239, 160, 123, 12, 119, 168, 35, 180, 191, 112, 75, 28, 135, 248, 179, 68, 79, 64, 
                                       91, 44, 215, 72, 3, 212, 31, 16, 171, 60, 103, 152, 147, 100, 47, 224, 187, 76, 183, 232, 
                                       99, 244, 255, 176, 139, 92, 199, 56, 243, 132, 143, 128, 155, 108, 23, 136, 67, 20, 95, 80,
                                       235, 124, 167, 216, 211, 164, 111, 32, 251, 140, 247, 40, 163, 52, 63, 240, 203, 156, 7, 
                                       120, 51, 196, 207, 192, 219, 172, 87, 200, 131, 84, 159, 144, 43, 188, 231, 24, 19, 228, 
                                       175, 96, 59, 204, 55, 104, 227, 116, 127, 48, 11, 220, 71, 184, 115, 4, 15, 0, 27, 236, 
                                       151, 8, 195, 148, 223, 208, 107, 252, 39, 88, 83, 36, 239, 160, 123, 12, 119, 168, 35, 
                                       180, 191, 112, 75, 28, 135, 248, 179, 68, 79, 64, 91, 44, 215, 72, 3, 212, 31, 16, 171, 
                                       60, 103, 152, 147, 100, 47, 224, 187, 76, 183, 232, 99, 244, 255, 176, 139, 92, 199, 56,
                                       243, 132, 143, 128, 155, 108, 23, 136, 67, 20, 95, 80, 235, 124, 167, 216, 211, 164, 111, 
                                       32, 251, 140, 247, 40, 163, 52, 63, 240, 203, 156, 7, 120, 51, 196, 207, 192, 219, 172, 87,
                                       200, 131, 84, 159, 144, 43, 188, 231, 24, 19, 26, 80, 158, 68, 50, 200, 150, 28, 10, 128, 
                                       206, 84, 34, 152, 70, 12, 122, 208, 254, 68, 18, 72, 246, 28, 106, 0, 46, 20, 2, 216, 166, 
                                       12, 90, 16, 94, 4, 114, 136, 86, 92, 74, 64, 142, 20, 98, 88, 6, 76, 58, 144, 190, 4, 82, 8, 
                                       182, 92, 42, 192, 238, 84, 66, 152, 102, 76, 26, 208, 30, 68, 50, 72, 22, 28, 10, 0, 78, 84, 
                                       34, 24, 198, 12, 122, 80, 126, 68, 18, 200, 118, 28, 106, 128, 174, 20, 2, 88, 38, 12, 90, 144,
                                       222, 4, 114, 8, 214, 92, 74, 192, 14, 20, 98, 216, 134, 76, 58, 16, 62, 4, 82, 136, 54, 92, 42, 
                                       64, 110, 84, 66, 24, 230, 76, 26, 80, 158, 68, 50, 200, 150, 28, 10, 128, 206, 84, 34, 152, 70, 
                                       12, 122, 208, 254, 68, 18, 72, 246, 28, 106, 0, 46, 20, 2, 216, 166, 12, 90, 16, 94, 4, 114, 136, 
                                       86, 92, 74, 64, 142, 20, 98, 88, 6, 76, 58, 144, 190, 4, 82, 8, 182, 92, 42, 192, 238, 84, 66, 152, 
                                       102, 76, 26, 208, 30, 68, 50, 72, 22, 28, 10, 0, 78, 84, 34, 24, 198, 12, 122, 80, 126, 68, 18, 200, 
                                       118, 28, 106, 128, 174, 20, 2, 88, 38, 12, 90, 144, 222, 4, 114, 8, 214, 92, 74, 192, 14, 20, 98, 216, 
                                       134, 76, 58, 16, 62, 4, 82, 136, 54, 92, 42, 64, 110, 84, 66, 24, 230, 76};
        public byte[] m_EncodeKey = null;
        public byte[] m_DecodeKey = null;

        public void InitKey()
        {
            m_EncodeKey = new byte[m_Key.Length];
            Buffer.BlockCopy(m_Key, 0, m_EncodeKey, 0, m_Key.Length);
            m_DecodeKey = new byte[m_Key.Length];
            Buffer.BlockCopy(m_Key, 0, m_DecodeKey, 0, m_Key.Length);
            m_EncodeIndex = 0;
            m_DecodeIndex = 0;
        }


        public void DecodePacket(ref byte[] key, int nlen)
        {
            byte dl;
            int n = 0, a, d;
            byte[] type;
            while (n < nlen)
            {
                dl = key[n];
                d = (int)dl >> 4 | (int)dl << 4;
                dl = (byte)d;
                key[n] = dl;
                type = BitConverter.GetBytes(m_DecodeIndex);
                a = type[1];
                dl -= m_DecodeKey[a + 768];
                key[n] = dl;
                d = m_DecodeKey[a + 256] ^ dl;
                dl = (byte)d;
                key[n] = dl;
                a = type[0];
                dl -= m_DecodeKey[a + 512];
                key[n] = dl;
                d = m_DecodeKey[a] ^ dl;
                dl = (byte)d;
                key[n] = dl;
                m_DecodeIndex++;
                n++;
            }
        }

        public void EncodePacket(ref byte[] key, int nlen)
        {
            byte dl;
            int n = 0, a;
            byte[] type;
            while (n < nlen)
            {
                type = BitConverter.GetBytes(m_EncodeIndex);
                a = type[0];
                dl = (byte)(key[n] ^ m_EncodeKey[a]);
                key[n] = (byte)dl;
                dl += m_EncodeKey[a + 512];
                key[n] = (byte)dl;
                a = type[1];
                dl = (byte)((int)dl ^ m_EncodeKey[a + 256]);
                key[n] = (byte)dl;
                dl += m_EncodeKey[a + 768];
                dl = (byte)((int)dl >> 4 | (int)dl << 4);
                key[n] = (byte)dl;
                m_EncodeIndex++;
                n++;
            }
        }

        public void SunUpdateKey(int key, int key2)
        {
     
        }
        public int ExchangeLongBits(int key, byte n)
        {
     
        }
        public void UpdateKey(int key)
        {
           

        }
         //   Debug.WriteLine(byteToText(m_DecodeKey));
           
        }

        public static String byteToText(byte[] array)
        {
            String ret = "{";
            for (int i = 0; i < array.Length; i++)
            {
                ret += array[i].ToString() + ",";
            }
            ret += "}";

            return ret;
        }

        public void GetKey(ref int key, ref int key2)
        {
            key = mKey1;
            key2 = mKey2;
        }

    }


    //Key生成类
    //2015.8.24
    public class GenerateKey
    {
        private static int key = 0;
        private static int key2 = 0;
        public static void Init(int _key = 0,int _key2 =0)
        {
            key = _key;
            key2 = _key2;
        }

        public static void GenerateKey_(ref int _key, ref int _key2)
        {
            key++;
            key2++;
            _key = key;
            _key2 = key2;
        }
    }

}
  
